package com.atgm.mall.gmmailmember.service.impl;

import com.atgm.common.exception.BizCodeEnume;
import com.atgm.common.exception.MemberException;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.common.utils.R;
import com.atgm.mall.gmmailmember.dao.MemberCollectArticleDao;
import com.atgm.mall.gmmailmember.dao.MemberDao;
import com.atgm.mall.gmmailmember.entity.MemberCollectArticleEntity;
import com.atgm.mall.gmmailmember.entity.MemberEntity;
import com.atgm.mall.gmmailmember.feign.ClubFeignService;
import com.atgm.mall.gmmailmember.service.MemberCollectArticleService;
import com.atgm.mall.gmmailmember.vo.resp.ArticleCollectMemberVo;
import com.atgm.mall.gmmailmember.vo.resp.ArticleCollectRespVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MemberCollectArticleServiceImpl extends ServiceImpl<MemberCollectArticleDao, MemberCollectArticleEntity> implements MemberCollectArticleService {

    @Autowired
    private ClubFeignService clubFeignService;

    @Autowired
    private MemberDao memberDao;

    @Override
    public Integer queryCollectCountByMemberId(Long memberId) {
        QueryWrapper<MemberCollectArticleEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("deleted", 0);  //获取未删除的文章收藏信息
        queryWrapper.eq("member_id", memberId);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public void saveMemberCollectArticle(MemberCollectArticleEntity memberCollectArticleEntity) {
        //检查该用户是否收藏该文章
        QueryWrapper<MemberCollectArticleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberCollectArticleEntity.getMemberId());
        queryWrapper.eq("article_id", memberCollectArticleEntity.getArticleId());
        MemberCollectArticleEntity selectOne = baseMapper.selectOne(queryWrapper);
        if (null != selectOne) {  //已收藏
            //取消收藏
            int count = baseMapper.deleteById(selectOne.getId());
            if (count != 1) {
                throw new MemberException("取消收藏失败", BizCodeEnume.MEMBER_COLLECT.getCode());
            }

            //收藏数量-1
            R r = clubFeignService.updateCollectCount(memberCollectArticleEntity.getArticleId(), 0);
            if (0 != r.getCode()) {
                throw new MemberException("取消收藏失败", BizCodeEnume.MEMBER_COLLECT.getCode());
            }
        }else {  //收藏
            memberCollectArticleEntity.setDeleted(0);
            memberCollectArticleEntity.setCreateTime(new Date());
            int count = baseMapper.insert(memberCollectArticleEntity);
            if (count != 1) {
                throw new MemberException("收藏失败", BizCodeEnume.MEMBER_COLLECT.getCode());
            }

            //收藏数量 +1
            R r = clubFeignService.updateCollectCount(memberCollectArticleEntity.getArticleId(), 1);
            if (0 != r.getCode()) {
                throw new MemberException("收藏失败", BizCodeEnume.MEMBER_COLLECT.getCode());
            }
        }
    }

    @Override
    public ArticleCollectRespVo queryCollectMemberByArticleId(Long articleId) {
        ArticleCollectRespVo articleCollectRespVo = new ArticleCollectRespVo();

        QueryWrapper<MemberCollectArticleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", articleId);

        Page<MemberCollectArticleEntity> page = new Page<MemberCollectArticleEntity>(1, 5);
        IPage<MemberCollectArticleEntity> iPage = baseMapper.selectPage(page, queryWrapper);

        articleCollectRespVo.setCount(iPage.getTotal());

        List<MemberCollectArticleEntity> records = iPage.getRecords();

        if (GmMallUtils.isNotEmpty(records)) {
            List<Long> selectMemberIdList = records.stream().map(MemberCollectArticleEntity::getMemberId).collect(Collectors.toList());
            QueryWrapper<MemberEntity> memberEntityQueryWrapper = new QueryWrapper<>();
            memberEntityQueryWrapper.select("id", "header");
            memberEntityQueryWrapper.in("id", selectMemberIdList);
            List<MemberEntity> memberEntities = memberDao.selectList(memberEntityQueryWrapper);

            List<ArticleCollectMemberVo> articleCollectMemberVos = new ArrayList<>();
            records.forEach(memberCollectArticleEntity -> {
                ArticleCollectMemberVo articleCollectMemberVo = new ArticleCollectMemberVo();
                articleCollectMemberVo.setId(memberCollectArticleEntity.getMemberId());
                articleCollectMemberVo.setHeader(memberEntities.stream().filter(memberEntity -> {
                    return memberEntity.getId().equals(memberCollectArticleEntity.getMemberId());
                }).findAny().get().getHeader());
                articleCollectMemberVos.add(articleCollectMemberVo);
            });
            articleCollectRespVo.setCollectList(articleCollectMemberVos);
        }

        return articleCollectRespVo;
    }

    @Override
    public Integer haveCollectArticle(Long articleId, Long memberId) {
        QueryWrapper<MemberCollectArticleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", memberId);
        queryWrapper.eq("article_id", articleId);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count;
    }
}
